/*
 * @Author: duchengdong
 * @Date: 2021-11-06 13:54:35
 * @LastEditors: yangmiaomiao
 * @LastEditTime: 2021-11-08 11:03:05
 * @Description:
 */
import { useAppSelector, useAppDispatch } from 'hooks'
import { PageInfo, ResObj } from 'types/config.type.d'
import {initPage} from 'data/common.data'
import { getStoreByName } from '../store'

const usePageList = (name: string): any[] => {
    const listStore = getStoreByName(name)
    const selectListState = listStore.selectListState
    const getList = listStore.getList
    const setSearchData = listStore.actions.setSearchData
    //state
    const { tableLoading, searchData, pageInfo, dataList } = useAppSelector(selectListState)
    //setstate
    const dispatch = useAppDispatch()

    //getList
    function getListHandle(pageInfo: PageInfo, searchData: ResObj) {
        dispatch(getList({ pageInfo, searchData }))
    }

    //查询
    function setSearchDataHandle(searchData: ResObj) {
        dispatch(setSearchData(searchData))
    }
    //重置
    function setResetDataHandle() {
        dispatch(setSearchData({}))
        getListHandle(initPage, {})
    }

    //delete
    function getListAfterDel() {
        let { pageSize, currentPage, totalRecords } = pageInfo
        currentPage = totalRecords % pageSize === 1 ? currentPage - 1 : currentPage
        currentPage = currentPage < 1 ? 1 : currentPage
        const page = {
            currentPage,
            pageSize,
        }
        dispatch(getList({ pageInfo: page, searchData }))
    }

    //加载状态、列表数据、页数&每页数量、 搜索内容、获取列表数据、 查询搜索内容的方法 、重置搜索内容的方法、删除列表之后的数据请求
    return [tableLoading, dataList, pageInfo, searchData, getListHandle, setSearchDataHandle, setResetDataHandle, getListAfterDel]
}

export default usePageList
